#include <simulation.h>
#include <stdio.h>
#include <config.h>
#include <pthread.h>
#include <mqueue.h>
#include <fcntl.h>           /* Pour les constantes O_* */
#include <sys/stat.h>        /* Pour les constantes « mode » */
#include <time.h>


#define NAME "/totoBatardConnard"
typedef struct lemsg{
	int i;
	long j;
	char c;
}msg;

void* unT()
{
	mqd_t laBAL; laBAL=mq_open(NAME,O_WRONLY); msg toto; 
	toto.i=12; 
	toto.j=4; 
	toto.c='B'; 
	mq_send(laBAL,(const char*)&toto,sizeof(msg),2); mq_close(laBAL
	); pthread_exit(0);
}

void* deuxT()
{
	mqd_t laBAL;
	laBAL=mq_open(NAME,O_RDONLY);
	msg toto;
	toto.i=4;
	printf("Retour receive : %d\n",mq_receive(laBAL,(char*)&toto,sizeof(msg),NULL));
	mq_close(laBAL);
	printf("le message contient : %d\n",toto.i);
	printf("le message contient : %d\n",toto.j);
	printf("le message contient : %c\n",toto.c);
	pthread_exit(0);
}

int main()
{
	pthread_t un, deux;
	mqd_t laBAL;
	struct mq_attr lesAttr={0,1,sizeof(msg),0};
	laBAL=mq_open(NAME,O_CREAT|O_RDWR,0777,&lesAttr);
	pthread_create(&un,NULL,unT,NULL);
	pthread_create(&deux,NULL,deuxT,NULL);
	pthread_join(deux,NULL);
	pthread_join(un,NULL);
	printf("Threads supprimés\n");
	mq_close(laBAL);
	mq_unlink(NAME);
	printf("BaL supprimée\n");
	return 0;
}
